package com.ktjiaoyu.service.impl;
import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.mapper.StorageMapper;
import com.ktjiaoyu.service.StorageService;
import com.ktjiaoyu.utils.Page;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @return 13141
 * 2025/6/25
 * 14:26
 */
@Service("storageService")
public class StorageServiceImpl implements StorageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class);
    @Resource
    private StorageMapper storageMapper;
    public void setStorageMapper(StorageMapper storageMapper) {
        this.storageMapper = storageMapper;
    }

    // 批量提交入库记录
    @Override
    @Transactional
    public int saveStorage(List<StorageRecord> storageRecordList) {
        int rows = 0;
        for (int i = 0; i < storageRecordList.size(); i++){
            // 拿出每一条用户数据插入
            StorageRecord storageRecord = storageRecordList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += storageMapper.addStorage(storageRecord);
        }
        // 返回总受影响操作
        return rows;
    }

    // 修改入库记录
    @Override
    @Transactional
    public int updateStorage(StorageRecord storageRecord) {
        return storageMapper.updateStorage(storageRecord);
    }

    // 删除入库记录
    @Override
    @Transactional
    public int deleteStorage(Long id) {
        return storageMapper.deleteStorage(id);
    }
    // 多条件查询入库记录分页数据
    @Override
    public Page<StorageRecord> findPage(StorageRecord entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<StorageRecord> page = null;
        try {
            page = new Page<StorageRecord>();
            page.setPageSize(pageSize);
//            查询总数，计算总页数
            Long count = storageMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
//            查询分页数据
            List<StorageRecord> data = storageMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行 StorageServiceImpl.findPage(Storage,Integer, Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }
    //    保存或修改
    @Override
    public boolean saveOrUpdate(StorageRecord entity) throws Exception {
        try {
            if (entity.getId() == null) {
//                无主键则新增
                return storageMapper.addStorage(entity) > 0;
            } else {
//                有主键则修改
                return storageMapper.updateStorage(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行 StorageServiceImpl.saveOrUpdate(Storage)方法出现异常，", e);
            throw e;
        }
    }
    //    根据 srCode 查询入库记录
    @Override
    public StorageRecord findBySrCode(String srCode) throws Exception {
        try {
            return storageMapper.getBySrCode(srCode);
        } catch (Exception e) {
            LOGGER.error("执行 StorageServiceImpl" + ".findBySrCode(String)方法出现异常.", e);
            throw e;
        }
    }
    //    根据 id 查询
    @Override
    public StorageRecord findById(Long id) throws Exception {
        try {
            return storageMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行 StorageServiceImpl" + ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }
}
